package com.newbie.cellsms.action;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.newbie.cellsms.bean.GsmRadioSetting;
import com.newbie.cellsms.bean.Message;
import com.newbie.cellsms.service.DbService;
import com.newbie.cellsms.service.NativeCallService;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@Results({ @Result(name = "preConfigInitSuccess", location = "/smsweb.jsp"),
		@Result(name = Action.INPUT, location = "/smsweb.jsp"),
		@Result(name = "settingSubmitSuccess", location = "/smsweb-confirm.jsp"),
		@Result(name = "settingConfirmSuccess", location = "/smsweb-sending.jsp"),
		@Result(name = "sendingShowSuccess", location = "/smsweb-sending.jsp"),
		@Result(name = "sendResultShowSuccess", location = "/smsweb-send-result.jsp") })
public class SetupAction extends ActionSupport {
	private static final long serialVersionUID = -8343307906569142345L;
	private GsmRadioSetting grs;
	// 消息列表
	private List<Message> messageList;
	// 短信条数
	private Integer count;
	private Date currentTime;
	private Date startTime;
	private static Integer pageStatus;

	public Date getStartTime() {
		return startTime;
	}

	public Date getCurrentTime() {
		return new Date();
	}

	public List<Message> getMessageList() {
		return messageList;
	}

	public GsmRadioSetting getGrs() {
		return grs;
	}

	public void setGrs(GsmRadioSetting grs) {
		this.grs = grs;
	}

	public Integer getCount() {
		return count;
	}

	private Map<String, Object> getApplication() {
		return ActionContext.getContext().getApplication();
	}

	public String input() throws ClassNotFoundException, SQLException {
		Class.forName("org.sqlite.JDBC");
		Connection connection = null;
		connection = DriverManager.getConnection("jdbc:sqlite:/c:/OpenBTS.db");
		Statement statement = connection.createStatement();
		statement.setQueryTimeout(30);
		ResultSet rs = statement.executeQuery("select * from config");
		while (rs.next()) {
			System.out.println("name = " + rs.getString("KEYSTRING"));
			System.out.println("id = " + rs.getString("VALUESTRING"));
		}
		return "";
	}

	private Integer getPS() {
		pageStatus = (Integer) getApplication().get("pageStatus");
		if (null == pageStatus) {
			pageStatus = 2;
		}
		return pageStatus;
	}

	private String choicePage() {
		System.out.println("choicePage00000000000" + pageStatus);
		switch (pageStatus) {
		case 1:
			this.getApplication().put("pageStatus", 2);
			return preConfigInit();
		case 2:
			this.getApplication().put("pageStatus", 3);
			return settingSubmit();
		case 3:
			this.getApplication().put("pageStatus", 4);
			return settingConfirm();
		case 4:
			this.getApplication().put("pageStatus", 5);
			return sendingShow();
		case 5:
			this.getApplication().put("pageStatus", 6);
			return sendResultShow();
		default:
			return preConfigInit();
		}
	}

	public String preConfigInit() {
		return "preConfigInitSuccess";
	}

	public String settingSubmit() {
		if (null != grs) {
			getApplication().put("grs", grs);
		} else {
			this.grs = (GsmRadioSetting) getApplication().get("grs");
		}
		return "settingSubmitSuccess";
	}

	public void validateSettingSubmit() {
		if (null != grs) {
			if (null == grs.getMsg() || grs.getMsg().length() == 0) {
				this.addFieldError("grs.msg", "内容不能为空");
			}
			if (null == grs.getArfcn() || grs.getArfcn().length() == 0) {
				this.addFieldError("grs.arfcn", "频点不能为空");
			}
			if (null == grs.getSrc() || grs.getSrc().length() == 0) {
				this.addFieldError("grs.src", "发送号码不能为空");
			}
		}
	}

	public String settingConfirm() {
		this.grs = (GsmRadioSetting) getApplication().get("grs");
		if (null == grs) {
			return "preConfigInitSuccess";
		}
		try {
			DbService.settingConfirm(grs);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		NativeCallService.startDevice();
		// return "settingConfirmSuccess";
		// 开始计时
		startTime = new Date();
		getApplication().put("startTime", startTime);
		return sendingShow();
	}

	/**
	 * 供实时查询发送情况
	 * 
	 * @return
	 */
	public String sendingShow() {
		try {
			Integer c = DbService.sendingShow();
			// 发送结束后,数据库中的数据会被清空,
			if (0 != c) {
				count = c;
			}
			getApplication().put("currentTime", (new Date()));
			getApplication().put("count", count);
			// getApplication().put("messageList", messageList);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return "sendingShowSuccess";
	}

	public String sendResultShow() {
		return "sendResultShowSuccess";
	}

	public String execute() throws Exception {
		return super.execute();
	}

}
